# -*- coding: utf-8 -*-
from . import getConnection


class Poety(object):

    def __init__(self, title, dynasty, author, content):
        self.title = title
        self.dynasty = dynasty
        self.author = author
        self.content = content

    def save(self):
        connection = getConnection()
        try:
            with connection.cursor() as cursor:
                sql = "INSERT INTO `poetry` (`title`, `dynasty`, `author`, `content`) value (%s, %s, %s, %s)"
                cursor.execute(sql, (self.title, self.dynasty, self.author, self.content))
            connection.commit()
        finally:
            connection.close()

    def __str__(self):
        return "title: {}, dynasty: {}, author: {}, content: {}".format(
            self.title, self.dynasty, self.author, self.content
        )
